package com.monlyu.action.deviceuse;

import java.util.List;

import com.monlyu.action.PageAction;
import com.monlyu.annotation.ActionAnno;
import com.monlyu.bean.Pages;
import com.monlyu.bo.DeviceuseBo;
import com.monlyu.util.DateUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.util.WebOptUtil;
import com.monlyu.vo.DeviceUse;
import com.monlyu.vo.Devices;
import com.monlyu.vo.User;

/**
 * @author SongJun
 * @since 2010-2-23
 * @description 设备使用申请
 */
public class EditDeviceuseAction extends PageAction {
	private DeviceuseBo deviceuseBo;
	private DeviceUse deviceuse;
	private Pages<User> users;

	public Pages<User> getUsers() {
		return users;
	}

	public void setUsers(Pages<User> users) {
		this.users = users;
	}

	public DeviceUse getDeviceuse() {
		return deviceuse;
	}

	public void setDeviceuse(DeviceUse deviceuse) {
		this.deviceuse = deviceuse;
	}

	public void setDeviceuseBo(DeviceuseBo deviceuseBo) {
		this.deviceuseBo = deviceuseBo;
	}

	@Override
	public String execute() throws Exception {
		users = deviceuseBo.getUserList(getDefPage());
		return view("editDeviceuse.jsp");
	}

	private Long appUserId;
	private Long appDeviceNo;
	private String appDeviceInfo;
	private String receUserid;
	private List<DeviceUse> dus;

	public void setDus(List<DeviceUse> dus) {
		this.dus = dus;
	}

	public String getReceUserid() {
		return receUserid;
	}

	public void setReceUserid(String receUserid) {
		this.receUserid = receUserid;
	}

	public String getAppDeviceInfo() {
		return appDeviceInfo;
	}

	public void setAppDeviceInfo(String appDeviceInfo) {
		this.appDeviceInfo = appDeviceInfo;
	}

	public Long getAppUserId() {
		return appUserId;
	}

	public void setAppUserId(Long appUserId) {
		this.appUserId = appUserId;
	}

	public Long getAppDeviceNo() {
		return appDeviceNo;
	}

	public void setAppDeviceNo(Long appDeviceNo) {
		this.appDeviceNo = appDeviceNo;
	}

	/**
	 * @author SongJun
	 * @since 2010-3-9
	 * @description 根据设备申请人或者设备申请号查询
	 */
	public String toQueryApp() {
		dus = deviceuseBo.getAppDeviceByUserNo(Devices.DEVICESTATUS_APP,
				appUserId, appDeviceNo);
		if (dus.size() > 0) {
			appDeviceInfo = "";
			for (DeviceUse du : dus) {
				appDeviceInfo += du.getDeviceid().getDeviceName() + "-"
						+ du.getDeviceid().getDeviceNo() + " | ";
			}
			if (null != appDeviceInfo) {
				DeviceUse du = dus.get(0);
				User user = du.getUserApp();
				String appno = du.getOutapp().getId().toString();
				appDeviceInfo += ";" + appno + ";" + du.getUse_startdate()
						+ ";" + du.getPlan_enddate() + ";"
						+ WebOptUtil.curUser().getName() + ";"
						+ du.getOutapp().getAduitor().getName();
				return ajax(appDeviceInfo);
			} else {
				return ajax("no");
			}
		} else {
			return ajax("no");
		}
	}

	/**
	 * @author SongJun
	 * @since 2010-2-27
	 * @description 设备发放
	 */
	@ActionAnno(readOnly = false)
	public String toApp() {
		dus = deviceuseBo.getAppDeviceByUserNo(Devices.DEVICESTATUS_APP,
				appUserId, appDeviceNo);
		if (dus.size() > 0) {
			if(StringUtil.isEmpty(receUserid)) {
				return ajax("设备领用人不能为空");
			}
			deviceuseBo.deviceRece(dus, deviceuse, new Long(receUserid));
			return ajax("成功领取设备个数为：" + dus.size() + " 个！");
		} else {
			return ajax("此人无申请记录或没有填写设备领用人！！！");
		}

	}

}
